<template>
  <div class="app-container">
    <el-menu :default-active="listQuery.tab" class="el-menu-demo" mode="horizontal" @select="handleSelect">
      <el-menu-item index="my">我的</el-menu-item>
      <el-menu-item index="waitCheck">待确认</el-menu-item>
      <el-menu-item index="all" v-if="allCargo">全部</el-menu-item>
      <el-menu-item index="company" v-if="companyCargo">公司</el-menu-item>
    </el-menu>
    <!--搜索-->
    <el-form :inline="true" :model="listQuery" size="mini" label-width="100px" style="margin-top:20px">
      <el-form-item label="采购合同编号：">
        <el-input v-model="listQuery.purchaseOrderSn" class="input-width" style="width:195px" clearable></el-input>
      </el-form-item>
      <el-form-item label="厂家牌号：">
        <el-input v-model="listQuery.goodsSn" class="input-width" style="width:195px" clearable></el-input>
      </el-form-item>
      <el-form-item label="交割时间：">
        <el-date-picker
          :default-time="['00:00:00', '23:59:59']"
          v-model="listQuery.lastCommitTime"
          type="daterange"
          format="yyyy 年 MM 月 dd 日"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="转移时间：">
        <el-date-picker
          :default-time="['00:00:00', '23:59:59']"
          v-model="listQuery.lastMoveTime"
          type="daterange"
          format="yyyy 年 MM 月 dd 日"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="当前货权人：">
        <el-select v-model="listQuery.ownerShipUserId" filterable clearable>
          <el-option
            v-for="item in userOptionList"
            :key="item.userId"
            :label="item.userName"
            :value="item.userId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="实际采购人：">
        <el-select  v-model="listQuery.purchaseUserId" filterable clearable>
          <el-option
            v-for="item in userOptionList"
            :key="item.userId"
            :label="item.userName"
            :value="item.userId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="转移展示过滤：">
        <el-select  v-model="listQuery.hasStock" clearable>
          <el-option label="全部展示" value=""></el-option>
          <el-option label="仅展示可转移" :value="1"></el-option>
          <el-option label="仅展示已全部转完" :value="0"></el-option>
        </el-select>
      </el-form-item>
       <el-form-item label="事业部：">
          <el-select v-model="listQuery.groupName" filterable clearable>
            <el-option
              v-for="(item,index) in groupSetList"
              :key="index"
              :label="item"
              :value="item">
            </el-option>
          </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          @click="handleSearchList()"
          type="primary">
          查询
        </el-button>
        <el-button
          v-if="userInfo.subscriberInfo && userInfo.subscriberInfo.departmentId == 3"
          @click="showAddHQ()"
          type="primary">
          新建货权
        </el-button>
      </el-form-item>
    </el-form>
    <div style="margin-top:20px">
      <el-radio-group  size="small" v-model="listQuery.goodsName" @change="handleSearchList">
        <el-radio-button label="">全部</el-radio-button>
        <el-radio-button :label="item.className" v-for="item in categoryList" :key="item.className">{{ item.className }}</el-radio-button>
      </el-radio-group>
    </div>
    <div class="table-container" v-show="this.summaryInfo.allOwnershipNum">
      <div style="margin-bottom: 10px;font-size:14px;color:red;">
        <span style="margin-right: 20px;">当前货权总数(吨):{{ this.summaryInfo.allOwnershipNum }}</span>
        <span style="margin-right: 20px;">已售(吨):{{ this.summaryInfo.allSaleNum }}</span>
        <span style="margin-right: 20px;">可售(吨):{{ this.summaryInfo.allStockNum }}</span>
      </div>
      <el-table
        v-loading="listLoading"
        :data="tableData"
        border
        style="width: 100%;">
        <el-table-column  label="采购合同编号" min-width="150" align="center">
          <template slot-scope="scope">
              {{scope.row.purchaseOrderSn}}
          </template>
        </el-table-column>
        <el-table-column  label="合同时间" min-width="150"  align="center">
          <template slot-scope="scope">
              {{scope.row.purchaseOrderTime}}
          </template>
        </el-table-column>
        <el-table-column  label="品类" min-width="100" align="center">
          <template slot-scope="scope">
            {{scope.row.className}}
          </template>
        </el-table-column>
        <el-table-column  label="厂家牌号" min-width="150" align="center">
          <template slot-scope="scope">
            {{scope.row.goodsSn}}
          </template>
        </el-table-column>
        <el-table-column  label="总数量" min-width="150" align="center">
          <template slot-scope="scope">
            {{scope.row.ownerShipNum}}
          </template>
        </el-table-column>
        <el-table-column  label="已售数量(财务已审核)" min-width="150" align="center">
          <template slot-scope="scope">
            {{scope.row.saleNum}}
          </template>
        </el-table-column>
        <el-table-column  label="可售数量" min-width="150" align="center">
          <template slot-scope="scope">
            {{scope.row.canSaleNum}}
          </template>
        </el-table-column>

        <el-table-column  label="当前市场价" min-width="150" align="center" v-if="listQuery.tab == 'my'">
          <template slot-scope="scope">
            {{scope.row.marketPrice}}
          </template>
        </el-table-column>

        <el-table-column  label="仓位盈亏" min-width="150" align="center" v-if="listQuery.tab == 'my'">
          <template slot-scope="scope">
            {{scope.row.stockProfit}}
          </template>
        </el-table-column>

        <el-table-column  label="目前底价" min-width="150" align="center" v-if="listQuery.tab == 'my' || listQuery.tab == 'waitCheck'">
          <template slot-scope="scope">
            {{scope.row.nowPrice}}
          </template>
        </el-table-column>

        <el-table-column  label="交割时间" min-width="150" align="center">
          <template slot-scope="scope">
            {{scope.row.lastCommitTime}}
          </template>
        </el-table-column>
        <el-table-column  label="转移时间" min-width="150" align="center">
          <template slot-scope="scope">
            {{scope.row.lastMoveTime}}
          </template>
        </el-table-column>
        <el-table-column  label="实际采购人" min-width="150" align="center">
          <template slot-scope="scope">
            {{scope.row.purchaseUserName}}
          </template>
        </el-table-column>
        <el-table-column  label="当前货权人" min-width="150" align="center">
          <template slot-scope="scope">
            {{scope.row.ownerShipUserName}}
          </template>
        </el-table-column>
        <el-table-column  label="是否自动转移" min-width="150" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.isPurchaseUser == 1">
                 <el-switch
                   @change="changeSet(scope.row)"
                   v-model="scope.row.isAutoMove"
                   :active-value="1"
                   :inactive-value="0">
                 </el-switch>
            </span>
            <span v-else>
                {{scope.row.isAutoMove == 1 ? '是':'否'}}
            </span>

          </template>
        </el-table-column>
        <el-table-column  label="是否限制下单" min-width="150" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.isPurchaseUser == 1">
                 <el-switch
                   @change="changeSet(scope.row)"
                   v-model="scope.row.isLimitOrder"
                   :active-value="1"
                   :inactive-value="0">
                 </el-switch>
            </span>
            <span v-else>
              {{scope.row.isLimitOrder == 1 ? '是':'否' }}
            </span>

          </template>
        </el-table-column>

        <el-table-column
          width="150"
          fixed="right"
          label="操作"
          align="center">
          <template slot-scope="scope">
            <el-button
              v-if="(scope.row.isCanMove && listQuery.tab != 'company') || (listQuery.tab == 'company' && ownershipReview && scope.row.ownerShipState != 0)"
              type="text"
              size="mini"
              @click="showMove(scope.row)">转移
            </el-button>
            <el-button
              v-if="(scope.row.isCanCheck && listQuery.tab != 'company') || (listQuery.tab == 'company' && ownershipReview && scope.row.ownerShipState == 0)"
              type="text"
              size="mini"
              @click="checkMore('succ',scope.row)">确认
            </el-button>
            <el-button
              v-if="(scope.row.isCanCheck && listQuery.tab != 'company') || (listQuery.tab == 'company' && ownershipReview && scope.row.ownerShipState == 0)"
              type="text"
              size="mini"
              @click="checkMore('refused',scope.row)">拒绝
            </el-button>
            <el-button
              v-if="(scope.row.isCanMove && listQuery.tab != 'company') || (listQuery.tab == 'company' && ownershipReview && scope.row.ownerShipState != 0)"
              type="text"
              size="mini"
              @click="showLog(scope.row)">日志
            </el-button>
            <el-button
              v-if="listQuery.tab == 'my'"
              type="text"
              size="mini"
              @click="showPrice(scope.row)">修改市场价
            </el-button>
            <el-button
              v-if="listQuery.tab == 'all' && scope.row.isCanBack == 1"
              type="text"
              size="mini"
              @click="back(scope.row)">回退
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[10,15,20]"
        :total="count">
      </el-pagination>
    </div>

    <!--货权转移-->
    <el-dialog
      :close-on-click-modal="false"
      title="货权转移"
      :visible.sync="dialogVisibleMove"
      width="460px">
      <el-form :model="moveForm" size="mini" label-width="100px" ref="moveForm" :rules="rules">
        <el-form-item label="品类：" style="margin-bottom: 0">{{moveForm.className}}</el-form-item>
        <el-form-item label="厂家牌号：" style="margin-bottom: 0">
          <el-select v-model="moveForm.sapGoodsId" filterable placeholder="请选择" clearable @change="changeGoods">
            <el-option
              v-for="(item,index) in goodsList2"
              :key="index"
              :label="item.goodsSn"
              :value="item.kingId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="可售数量：" style="margin-bottom: 0">{{moveForm.canSaleNum}}</el-form-item>
        <el-form-item label="转给人：" prop="toChargeId">
          <el-select  v-model="moveForm.toChargeId" filterable clearable style="width:100%" @change="getOrderList">
            <el-option
              v-for="item in userOptionList"
              :key="item.userId"
              :label="item.userName"
              :value="item.userId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="超卖结算：" prop="orderDetailId">
          <el-select v-model="moveForm.orderDetailId"  placeholder="请先选择转给人" clearable multiple style="width: 100%" @change="sumOrderNum">
            <el-option :value="item.orderDetailId" v-for="item in orderList" :key="item.orderDetailId" :label="item.orderSn">订单编号：{{item.orderSn}}，订单商品数量：{{item.goodsNum}}</el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="转移数量：" prop="moveNum" >
          <el-input v-model="moveForm.moveNum" type="number" :disabled="moveForm.orderDetailId.length > 0"></el-input>
        </el-form-item>
        <div style="color:red;font-size: 12px;padding-left:100px;">请填写现汇价，目前承兑汇率{{purchaseCashPriceRate}}</div>
        <el-form-item label="转移价格：" prop="movePrice">
          <el-input v-model="moveForm.movePrice" type="number" clearable></el-input>
        </el-form-item>
        <el-form-item label="交割时间：" prop="commitTime" v-if="moveForm.orderDetailId.length == 0">
          <el-date-picker
            style="width:100%"
            v-model="moveForm.commitTime"
            type="date"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="handleConfirm" :loading="btnLoading">确 定</el-button>
      </span>
    </el-dialog>

    <!--日志-->
    <el-dialog
      :close-on-click-modal="false"
      title="日志"
      :visible.sync="dialogVisibleLog"
      width="460px">
       <div style="max-height: 400px;overflow-y: auto;padding:10px 10px 10px 0"  v-loading="logLoading">
         <el-timeline>
           <el-timeline-item
             v-for="(item, index) in logList"
             :key="index">
                 <div style="font-size:12px;">{{item.operateTime}} <span style="padding-left:10px;">{{item.operateUserName}}</span></div>
                 <div style="font-size: 12px;line-height: 20px"> {{item.operateContent}} </div>
           </el-timeline-item>
         </el-timeline>
         <div style="color:#888;padding:20px;text-align: center;font-size:12px" v-if="logList.length == 0 && !logLoading">- 暂无数据 -</div>
       </div>
    </el-dialog>

    <!--修改市场价-->
    <el-dialog
      :close-on-click-modal="false"
      title="修改市场价"
      :visible.sync="dialogVisiblePrcie"
      width="460px">
      <el-form :model="priceForm" size="mini" label-width="100px" ref="priceForm" :rules="rules1">

        <el-form-item label="市场价格：" prop="marketPrice">
          <el-input v-model="priceForm.marketPrice" type="number" clearable></el-input>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
          <el-button @click="prcieHandleClose">取 消</el-button>
          <el-button type="primary" @click="prcieHandleConfirm" :loading="btnPrcieLoading">确 定</el-button>
      </span>
    </el-dialog>

    <!--新建货权-->
    <el-dialog
      :close-on-click-modal="false"
      title="新建货权"
      :visible.sync="dialogVisibleAdd"
      width="400px">
      <el-form :model="addForm" size="mini" label-width="100px" ref="addForm" :rules="rules2">
        <el-form-item label="厂家牌号：" prop="sapGoodsId">
          <el-select v-model="addForm.sapGoodsId" filterable placeholder="请选择" clearable @change="changeGoods">
            <el-option
              v-for="(item,index) in goodsList"
              :key="index"
              :label="item.goodsSn"
              :value="item.kingId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="品类：">{{addForm.goodsClass}}</el-form-item>
        <el-form-item label="数量：" prop="goodsNum">
          <el-input v-model="addForm.goodsNum" type="number" clearable></el-input>
        </el-form-item>
        <el-form-item label="价格：" prop="goodsPrice">
          <el-input v-model="addForm.goodsPrice" type="number" clearable></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
          <el-button @click="handleClose2">取 消</el-button>
          <el-button type="primary" @click="handleConfirm2" :loading="btnLoading2">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>
<script>
import { getOrderUsersList,orderGoodsList } from '@/api/partner';
import {addOwnership,ownershipSet,ownershipBack}from '@/api/company';
import {
  getOwnerShipList,
  getGoodsClassList,
  ownerShipMove,
  ownerShipCheck,
  ownerShipLog,
  myOrderList,
  changeMarketPrice,
  psMergeConfig,
  groupSetListAPI,
} from '@/api/order';
import store from '@/store';
import { mapGetters } from 'vuex';
const defaultListQuery = {
  pageNum: 1,
  pageSize: 10,
  goodsName: "",  //商品分类
  goodsSn: "", //商品牌号
  lastMoveTime: [],  //转移时间
  lastCommitTime: [],//交割时间
  ownerShipUserId: '', //货权人ID
  purchaseUserId: '', //实际采购人ID
  hasStock: '',//转移展示过滤
  groupName: '',
  tab: 'my',
  purchaseOrderSn: ''
};
const defaultMoveForm = {
  className: '',
  goodsSn: '',
  ownerShipId: '',   //货权单ID
  movePrice: '', //转移价格
  moveNum: "",      //转移数量
  commitTime: "", //交割时间
  toChargeId: '',  //转给人
  orderDetailId: [], //订单明细ID
  canSaleNum: '',
  sapGoodsId:''
};
const defaultPriceForm = {
  ownerShipId: '',   //货权单ID
  marketPrice: '', //转移价格
};
const defaultAddForm = {
  "sapGoodsId" :'', //SAP商品ID
  "goodsNum" : '', //数量
  "goodsPrice" : '',//价格
  goodsClass:''
};
export default {
  name: "index",
  data() {
    const validateMoveNum = (rule, value, callback) => {
      if (value == '') {
        callback(new Error('此项必填'))
      } else if (value > this.moveForm.canSaleNum) {
        callback(new Error('转移数量不能大于可售数量'))
      } else {
        callback()
      }
    };
    const validateOrderDetailId = (rule, value, callback) => {
      let sum = 0
      if (value.length > 0) {
        value.forEach((el) => {
          let index = this.orderList.findIndex((ee) => {
            return ee.orderDetailId == el
          })
          if (index != -1) {
            sum = sum + parseFloat(this.orderList[index]['goodsNum'])
          }
        })
      }
      if (sum > this.moveForm.moveNum) {
        callback(new Error('所选所订单商品数量大于转移数量'))
      } else {
        callback()
      }
    };
    return {
      listQuery: JSON.parse(JSON.stringify(defaultListQuery)),
      tableData: [],
      summaryInfo: {},
      count: 0,
      listLoading: false,
      categoryList: [],
      groupSetList: [],
      btnLoading: false,
      moveForm: JSON.parse(JSON.stringify(defaultMoveForm)),
      rules: {
        movePrice: [{ required: true, message: '此项必填', trigger: 'blur' },],
        moveNum: [{ required: true, trigger: 'blur', validator: validateMoveNum },],
        commitTime: [{ required: true, message: '此项必填', trigger: 'change' },],
        toChargeId: [{ required: true, message: '此项必填', trigger: 'change' },],
        orderDetailId: [{ trigger: 'change', validator: validateOrderDetailId }]
      },
      dialogVisibleMove: false,
      dialogVisibleLog: false,
      logList: [],
      userOptionList: [],
      orderList: [],
      sapGoodsId: '',
      logLoading: false,
      roles: store.getters.roles ? store.getters.roles : [],
      dialogVisiblePrcie: false,
      btnPrcieLoading: false,
      rules1: {
        marketPrice: [{ required: true, message: '此项必填', trigger: 'blur' },],
      },
      priceForm: JSON.parse(JSON.stringify(defaultPriceForm)),
      purchaseCashPriceRate: '',
      userInfo: {},
      dialogVisibleAdd:false,
      goodsList:[],
      addForm:JSON.parse(JSON.stringify(defaultAddForm)),
      btnLoading2:false,
      rules2:{
        sapGoodsId: [{ required: true, message: '此项必填', trigger: 'change' },],
        goodsNum: [{ required: true, message: '此项必填', trigger: 'blur' },],
        goodsPrice: [{ required: true, message: '此项必填', trigger: 'blur' },],
      },
      goodsList2:[]
    }
  },
  components: {

  },
  computed: {
    ...mapGetters([
      'name',
      'mobile',
      'userId'
    ]),

    ownershipReview() {
      if (this.roles.indexOf("ownershipReview") != -1) {
        return true
      } else {
        return false
      }
    },
    allCargo() {
      if (this.roles.indexOf("allCargo") != -1) {
        return true
      } else {
        return false
      }
    },
    companyCargo() {
      if (this.roles.indexOf("companyCargo") != -1) {
        return true
      } else {
        return false
      }
    },
  },
  created() {
    this.userInfo = localStorage.getItem('userInfo') ? JSON.parse(localStorage.getItem('userInfo')).data : {}
    console.log(this.userInfo)
    this.getUserOptionList()
    this.getClassList()
    this.getList()
    this.getHui()
    this.getGroupSetList()
    orderGoodsList().then((res)=>{
      this.goodsList = res.list
    })
  },
  filters: {
  },
  methods: {
    back(item){
      this.$confirm('确定回退此项货权?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        ownershipBack({ownershipId:item.ownerShipId,}).then(()=>{
          this.getList();
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        });
      });
    },
    changeSet(item){
      ownershipSet({ownershipId:item.ownerShipId,isAutoMove:item.isAutoMove,isLimitOrder:item.isLimitOrder}).then(()=>{
        this.$message({
          type: 'success',
          message: '操作成功!'
        });
        this.getList();
      }).catch(()=>{
        this.getList();
      })
    },
    handleClose2(){
      this.dialogVisibleAdd = false
    },
    handleConfirm2(){
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          this.btnLoading2 = true;
          let data = JSON.parse(JSON.stringify(this.addForm))
          addOwnership(data).then((res) => {
            this.$message({
              type: 'success',
              message: '操作成功!'
            });
            this.btnLoading2 = false;
            this.handleClose2();
            this.getList();
          }).catch(() => {
            this.btnLoading2 = false;
          })
        }
      })
    },
    showAddHQ(){
      this.addForm = JSON.parse(JSON.stringify(defaultAddForm))
      this.dialogVisibleAdd = true;
    },
    //切换商品
    changeGoods(){
      let i = this.goodsList.findIndex((el)=>{
        return el.kingId == this.addForm.sapGoodsId
      })
      console.log(i)
      if(i != -1){
        this.addForm.goodsClass = this.goodsList[i]['goodsClass']
      }
    },
    //获取小组列表
    getGroupSetList() {
      groupSetListAPI().then((res) => {
        this.groupSetList = res.groupList;
      })
    },
    getHui() {
      psMergeConfig().then((res) => {
        this.purchaseCashPriceRate = res.purchaseCashPriceRate
      })
    },
    sumOrderNum() {
      if (this.moveForm.orderDetailId.length > 0) {
        let sum = 0
        this.moveForm.orderDetailId.forEach((el) => {
          let index = this.orderList.findIndex((ee) => {
            return ee.orderDetailId == el
          })
          if (index != -1) {
            sum = sum + parseFloat(this.orderList[index]['goodsNum'])
          }
        })
        this.moveForm.moveNum = sum
      } else {
        this.moveForm.moveNum = ''
      }
    },

    //显示修改价格
    showPrice(item) {
      this.dialogVisiblePrcie = true
      this.priceForm = JSON.parse(JSON.stringify(defaultPriceForm))
      this.priceForm.ownerShipId = item.ownerShipId
    },
    prcieHandleClose() {
      this.$refs.priceForm.clearValidate()
      this.dialogVisiblePrcie = false;
      this.btnPrcieLoading = false;
    },
    prcieHandleConfirm() {
      this.$refs.priceForm.validate((valid) => {
        if (valid) {
          this.btnPrcieLoading = true;
          let data = JSON.parse(JSON.stringify(this.priceForm))
          changeMarketPrice(data).then((res) => {
            this.$message({
              type: 'success',
              message: '操作成功!'
            });
            this.btnPrcieLoading = false;
            this.prcieHandleClose();
            this.getList();
          }).catch(() => {
            this.btnPrcieLoading = false;
          })
        }
      })
    },
    //显示日志
    showLog(item) {
      this.dialogVisibleLog = true
      this.logLoading = true
      ownerShipLog({ ownerShipId: item.ownerShipId, pageSize: 1000, pageNum: 1 }).then((res) => {
        this.logList = res.list
        this.logLoading = false
      }).catch(() => {
        this.logLoading = false
      })
    },
    //确认/拒绝转移
    checkMore(type, item) {
      let msg = ''
      if (type == 'succ') {
        msg = '确定通过此项货权转移吗？'
      } else {
        msg = '确定拒绝此项货权转移吗？'
      }
      this.$confirm(msg, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        ownerShipCheck({ ownerShipId: item.ownerShipId, checkState: type }).then((res) => {
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          this.getList()
        })
      }).catch(() => { })
    },
    //获取品名列表
    getClassList() {
      getGoodsClassList().then((res) => {
        this.categoryList = res
      })
    },
    //获取员工下拉选项
    getUserOptionList() {
      getOrderUsersList({ status: 1 }).then((res) => {
        this.userOptionList = res;
      })
    },

    //获取超买订单
    getOrderList() {
      myOrderList({ sapGoodsId: this.sapGoodsId, userId: this.moveForm.toChargeId }).then((res) => {
        this.orderList = res
      })
    },


    //转移
    showMove(item) {
      this.dialogVisibleMove = true;
      this.moveForm = JSON.parse(JSON.stringify(defaultMoveForm))
      this.moveForm.className = item.className;
      this.moveForm.goodsSn = item.goodsSn;
      this.moveForm.sapGoodsId = item.sapGoodsId
      this.moveForm.movePrice = item.nowPrice;
      this.moveForm.ownerShipId = item.ownerShipId;
      this.moveForm.canSaleNum = item.canSaleNum
      this.sapGoodsId = item.sapGoodsId
      this.orderList = []
      this.goodsList2 = []
      orderGoodsList({goodsName:item.className}).then((res)=>{
        this.goodsList2 = res.list
      })
    },

    //关闭转移
    handleClose() {
      this.$refs.moveForm.clearValidate()
      this.dialogVisibleMove = false;
      this.btnLoading = false;
    },

    //确认转移
    handleConfirm() {
      this.$refs.moveForm.validate((valid) => {
        if (valid) {
          this.btnLoading = true;
          let data = JSON.parse(JSON.stringify(this.moveForm))
          delete data.className;
          delete data.goodsSn;
          delete data.canSaleNum
          if (this.moveForm.orderDetailId.length > 0) {
            delete data.commitTime
          }
          ownerShipMove(data).then((res) => {
            this.$message({
              type: 'success',
              message: '操作成功!'
            });
            this.btnLoading = false;
            this.handleClose();
            this.getList();
          }).catch(() => {
            this.btnLoading = false;
          })
        }
      })
    },

    /**
     * 切换选择状态
     */
    handleSelect(key, keyPath) {
      this.listQuery = JSON.parse(JSON.stringify(defaultListQuery))
      this.listQuery.tab = key;
      this.handleSearchList();
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },

    handleSearchList() {
      this.listQuery.pageNum = 1;
      this.getList();
    },

    //获取列表
    getList() {
      this.listLoading = true;
      getOwnerShipList(this.listQuery).then(response => {
        this.tableData = response.list;
        this.summaryInfo = response.summaryInfo;
        this.count = response.count;
        this.listLoading = false;
      }).catch(() => {
        this.listLoading = false;
      });
    },
  },
};
</script>


